package ss12.ColorIdentifier;
import java.util.*;
import android.graphics.*;
public class HexToStringConverter {
	private HashMap<Integer, String> colors;
	public HexToStringConverter(){
		populateHashMap();
	}
	private void populateHashMap(){
		colors = new HashMap<Integer, String>();
		colors.put(new Integer(0xFF0000), "Red");
		colors.put(new Integer(0xFFA500), "Orange");
		colors.put(new Integer(0xFFFF00), "Yellow");
		colors.put(new Integer(0xFF00FF), "Magenta");
		colors.put(new Integer(0x008000), "Green");
		colors.put(new Integer(0x0000FF), "Blue");
		colors.put(new Integer(0x800080), "Purple");
		colors.put(new Integer(0xFFFFFF), "White");
		colors.put(new Integer(0x000000), "Black");
		colors.put(new Integer(0x888888), "Grey");
		colors.put(new Integer(0x00FF00), "Lime");
		colors.put(new Integer(0x854513), "Brown");
		colors.put(new Integer(0xFFC0CB), "Pink");
		colors.put(new Integer(0x006400), "Dark Green");
		colors.put(new Integer(0x00FFFF), "Cyan");
		//colors.put(new Integer(0xFF), value)
	}
	public String getColorName(int rgbColor){
		int dr, dg, db;
		int closestColor = 0;
		int closestDistance = 1000000;
		int r = Color.red(rgbColor);
		int g = Color.green(rgbColor);
		int b = Color.blue(rgbColor);
		float distance;
		for(Integer i: colors.keySet()){
			//dr = Math.abs(Color.red(i.intValue()) - r);
			//dg = Math.abs(Color.green(i.intValue()) - g);
			//db = Math.abs(Color.blue(i.intValue()) - b);
			dr = Color.red(i.intValue()) - r;
			dg = Color.green(i.intValue()) - g;
			db = Color.blue(i.intValue()) - b;
			distance = dr * dr + dg * dg + db * db;
			if(distance < closestDistance){
				closestDistance = (int)distance;
				closestColor = i.intValue();
			}
			
		}
		return colors.get(closestColor);
	}
}
